package com.study.lu;

public class MaxAreaTest {
    public static void main(String[] args) {
        int[] height = new int[]{1,8,6,2,5,4,8,3,7};
        MaxAreaTest maxAreaTest = new MaxAreaTest();
        System.out.println(maxAreaTest.maxArea(height));

    }

    public int maxArea(int[] height) {
        if(null == height || height.length==0){
            return 0;
        }
        if(height.length==1){
            return height[0];
        }

        int headIndex = 0;
        int tailIndex = height.length-1;

        int maxArea = 0;

        while (headIndex!=tailIndex){
            int leftHeight = height[headIndex];
            int rightHeight = height[tailIndex];
            int width = tailIndex-headIndex;
            if(leftHeight>rightHeight){
                tailIndex--;
            }else {
                headIndex++;
            }
            int minHeight = leftHeight>rightHeight?rightHeight:leftHeight;
            int tempArea = width*minHeight;
            maxArea = tempArea>maxArea?tempArea:maxArea;
        }

        return maxArea;


    }
}
